package android.slc.extras.user.repository.remote;

import android.slc.adapter.PageModel;
import android.slc.adapter.SelectBox;
import android.slc.adapter.SelectUtils;
import android.slc.appbase.api.slc.callback.po.SlcEntity;
import android.slc.appbase.repository.remote.PageTransformUtils;
import android.slc.appbase.repository.remote.SelectTransformUtils;
import android.slc.extras.user.entity.DeptAndUser;
import android.slc.extras.user.entity.SysDept;
import android.slc.extras.user.entity.UserInfo;
import android.slc.or.SlcNu;
import android.slc.or.SlcParam;
import android.slc.rx.SlcRxJavaUtils;

import com.blankj.utilcode.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Function;

/**
 * @author slc
 * @date 2020-08-25 17:50
 */
public class DeptServiceRepository {
    /**
     * 获取部门列表
     *
     * @return
     */
    public static Observable<SlcEntity<List<SysDept>>> list(long parentId) {
        return SlcNu.getInstance().create(DeptService.class)
                .list(SlcParam.newBuilder().put("parentId", parentId).build())
                .compose(SlcRxJavaUtils.applyOoAndroidSchedulers());
    }

    /**
     * 获取部门列表
     *
     * @return
     */
    public static Observable<SlcEntity<PageModel<SysDept>>> page(long parentId, int current) {
        return SlcNu.getInstance().create(DeptService.class)
                .list(SlcParam.newBuilder().put("parentId", parentId).build())
                .map(PageTransformUtils.list2PageModelMap())
                .compose(SlcRxJavaUtils.applyOoAndroidSchedulers());
    }

    /**
     * 获取部门信息
     *
     * @return
     */
    public static Observable<SlcEntity<SysDept>> getInfo(long deptId) {
        return SlcNu.getInstance().create(DeptService.class)
                .getInfo(deptId)
                .compose(SlcRxJavaUtils.applyOoAndroidSchedulers());
    }

    /**
     * DeptAndUserVo 转list
     *
     * @return
     */
    public static Function<SlcEntity<DeptAndUser>, SlcEntity<List<Serializable>>> data2ListGroup() {
        return data2ListGroup(false);
    }

    /**
     * DeptAndUserVo 转list
     *
     * @return
     */
    public static Function<SlcEntity<DeptAndUser>, SlcEntity<List<Serializable>>> data2ListGroup(boolean selectModel) {
        return data2ListGroup(selectModel, null);
    }

    /**
     * DeptAndUserVo 转list
     *
     * @return
     */
    public static Function<SlcEntity<DeptAndUser>, SlcEntity<List<Serializable>>> data2ListGroup(List<UserInfo> userInfoList) {
        return data2ListGroup(false, userInfoList);
    }

    /**
     * DeptAndUserVo 转list
     *
     * @return
     */
    public static Function<SlcEntity<DeptAndUser>, SlcEntity<List<Serializable>>> data2ListGroup(boolean selectModel, List<UserInfo> userInfoList) {
        return deptAndUserVoSlcEntity -> {
            SlcEntity<List<Serializable>> listSlcEntity = new SlcEntity<>();
            listSlcEntity.setCode(deptAndUserVoSlcEntity.getCode());
            listSlcEntity.setMsg(deptAndUserVoSlcEntity.getMsg());
            if (deptAndUserVoSlcEntity.isSuccess()) {
                List<Serializable> dataList = new ArrayList<>();
                dataList.addAll(deptAndUserVoSlcEntity.getData().getNodeDeptList());
                if (selectModel) {
                    List<SelectBox<UserInfo>> userSelectBoxList = SelectTransformUtils.itemTransitionSelectBox(deptAndUserVoSlcEntity.getData().getUserList(), false);
                    SelectUtils.fillSelect(userInfoList, userSelectBoxList);
                    dataList.addAll(userSelectBoxList);
                } else if (CollectionUtils.isNotEmpty(deptAndUserVoSlcEntity.getData().getUserList())) {
                    dataList.addAll(deptAndUserVoSlcEntity.getData().getUserList());
                }
                listSlcEntity.setData(dataList);
            }
            return listSlcEntity;
        };
    }

}
